package leetcode.dp;

/**
 * 198. 打家劫舍
 */
public class Rob {
    public static void main(String[] args) {
        int[] arr = {2, 7, 9, 3, 1};
        int[] arr1 = {1, 2, 3, 1};
        int[] arr2 = {1, 3, 1, 3, 100};
        System.out.println(rob(arr2));
    }

    public static int rob(int[] nums) {
        int len = nums.length;
        int[] dp = new int[len];
        if (len == 0) {
            return 0;
        }
        if (len == 1) {
            return nums[0];
        }
        if (len == 2) {
            return Math.max(nums[0], nums[1]);
        }
        if (len == 3) {
            return Math.max(nums[0] + nums[2], nums[1]);
        }

        dp[0] = nums[0];
        dp[1] = Math.max(nums[0], nums[1]);
        dp[2] = Math.max(nums[0] + nums[2], nums[1]);
        for (int i = 3; i < len; i++) {
            dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 3] + nums[i]);
        }
        return Math.max(dp[len - 1], dp[len - 2]);
    }
}
